package com.adtech.farmProducts.adapter;

import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.TextView;

import com.adtech.common.widgets.recycler.RecyclerViewAdapter;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.model.ProvinceInfo;


/**
 * 消息配置-机构配置
 */
public class ProvinceAdapter extends RecyclerViewAdapter<ProvinceInfo> {


    private boolean isCheck;

    /**
     * 构造方法，初始化监听器
     *
     * @param adapterListener
     */
    public ProvinceAdapter(AdapterListener<ProvinceInfo> adapterListener) {
        super(adapterListener);
    }

    @Override
    protected int getItemViewType(int position, ProvinceInfo data) {
        return R.layout.item_sort;
    }


    @Override
    protected ViewHolder<ProvinceInfo> getViewHolder(View view, int viewType) {

        return new MerchantViewHolder(view);
    }

    /**
     * 是否显示复选框
     */
    public void isCheck(boolean isChecks) {
        isCheck = isChecks;
    }


    /**
     * 定义内部的ViewHolder类，用于显示控件创建的复用
     */
    private class MerchantViewHolder extends ViewHolder<ProvinceInfo> {
        private AppCompatImageView mSelectView;
        private TextView tvName;
        private TextView tvLetter;

        /**
         * 构造方法
         *
         * @param itemView View
         */
        public MerchantViewHolder(View itemView) {
            super(itemView);
            mSelectView = itemView.findViewById(R.id.iv_select);
            tvName = itemView.findViewById(R.id.tv_name);
            tvLetter = itemView.findViewById(R.id.tv_letter);
        }

        @Override
        protected void onBind(ProvinceInfo data, int position) {
            // 如果为true则显示用于选择Item删除条目的View，否则则隐藏
            if (isCheck) {
                mSelectView.setVisibility(View.VISIBLE);
            } else {
                mSelectView.setVisibility(View.GONE);
            }
//            int section = data.getSortLetters().charAt(0);
//            if (position == getPositionForSection(section)) {
//                tvLetter.setVisibility(View.VISIBLE);
//                tvLetter.setText(data.getSortLetters());
//            } else {
//                tvLetter.setVisibility(View.GONE);
//            }
            tvLetter.setVisibility(View.GONE);
            tvName.setText(data.getName());
        }

        @Override
        public boolean isNeedClick() {
            return true;
        }
    }

    public int getPositionForSection(int section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = getDataList().get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

}
